'use strict'
const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')
function resolve(dir) {
  return path.join(__dirname, dir)
}
const name = '渊联'
const port = process.env.port || process.env.npm_config_port || 9000

const plugins = [
  new CopyWebpackPlugin([
    {
      from: 'node_modules/@liveqing/liveplayer/dist/component/crossdomain.xml'
    },
    { from: 'node_modules/@liveqing/liveplayer/dist/component/liveplayer.swf' },
    {
      from: 'node_modules/@liveqing/liveplayer/dist/component/liveplayer-lib.min.js',
      to: 'js/'
    }
  ])
]

module.exports = {
  publicPath: process.env.NODE_ENV === 'development' ? '/' : '././',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: false,
  productionSourceMap: false,
  devServer: {
    host:"0.0.0.0",
    port: port,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      [process.env.VUE_APP_NGINX_PATH]: {
        target: process.env.VUE_APP_BASE_API,
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          ['^' + process.env.VUE_APP_NGINX_PATH]: ''
        }
      },
      [process.env.VUE_APP_BASE_PATH]:{
        target:process.env.VUE_APP_DEVELOPMENT_SERVER_ADDRESS,
        ws:true,
        changeOrigin:true,
        pathRewrite:{
          ["^" + process.env.VUE_APP_BASE_PATH]:""
        }
      },
      "/junyue-api":{
        target:"http://192.168.1.52:20900",
        ws:true,
        changeOrigin:true,
        pathRewrite:{
          "^/junyue-api":""
        }
      },
    }
  },
  configureWebpack: {
    plugins,
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    devtool:"source-map"
  },
  chainWebpack(config) {
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])
    config.module.rule('svg').exclude.add(resolve('src/icons')).end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.when(process.env.NODE_ENV !== 'development', (config) => {
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [
          {
            inline: /runtime\..*\.js$/
          }
        ])
        .end()
      config.optimization.splitChunks({
        chunks: 'all',
        minSize: 30000, // 字节 引入的文件大于30kb才进行分割
        minChunks: 1, // 模块至少使用次数
        maxAsyncRequests: 5, // 同时加载的模块数量最多是5个，只分割出同时引入的前5个文件
        maxInitialRequests: 3, // 首页加载的时候引入的文件最多3个
        automaticNameDelimiter: '~', // 缓存组和生成文件名称之间的连接符
        name: true,
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' // only package third parties that are initially dependent
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), // can customize your rules
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
      config.optimization.runtimeChunk('single')
    })
  }
}
